import 'dart:io';

import 'package:ai_model/app/res/colors.dart';
import 'package:ai_model/app/routes/app_pages.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../controllers/login_controller.dart';

class LoginView extends GetView<LoginController> {
  const LoginView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // 设置状态栏为透明
    SystemChrome.setSystemUIOverlayStyle(
      const SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        statusBarIconBrightness: Brightness.dark,
      ),
    );

    // 在页面构建后检查版本更新
    WidgetsBinding.instance!.addPostFrameCallback((_) {
      // BldFerrisWheel.checkUpgrade(context);
    });

    return Scaffold(
      backgroundColor: Colors.white,
      resizeToAvoidBottomInset: false,
      body: GestureDetector(
        onTap: () => FocusScope.of(context).unfocus(),
        child: Stack(
          children: [
            Positioned.fill(
              child: Image.asset(
                'assets/images/bg_login.webp',
                fit: BoxFit.fitWidth,
                alignment: Alignment.topCenter,
                width: double.infinity,
              ),
            ),
            SafeArea(
              child: SingleChildScrollView(
                child: Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Padding(
                        padding: EdgeInsets.only(left: 22.r, top: 94.h),
                        child: Row(
                          children: [
                            Image.asset(
                              'assets/images/icon_app_logo.png',
                              width: 60.r,
                              height: 60.r,
                            ),
                            SizedBox(width: 16.w),
                            Text(
                              "hello，\n欢迎来到Wo慧访",
                              style: TextStyle(
                                fontSize: 26.67.sp,
                                fontWeight: FontWeight.bold,
                                color: const Color(0xFF262626),
                                height: 1.2,
                              ),
                            ),
                          ],
                        ),
                      ),
                      SizedBox(height: 60.h),
                      Padding(
                        padding: EdgeInsets.symmetric(horizontal: 22.r),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            // 手机号标题
                            Row(
                              children: [
                                Image.asset(
                                  'assets/images/icon_edit_phone.webp',
                                  width: 18.r,
                                  height: 18.r,
                                ),
                                SizedBox(width: 4.w),
                                Text(
                                  '手机号',
                                  style: TextStyle(
                                    fontSize: 18.sp,
                                    fontWeight: FontWeight.w500,
                                    color: const Color(0xFF262626),
                                  ),
                                ),
                              ],
                            ),
                            // SizedBox(height: 8.h),
                            // 手机号输入框
                            SizedBox(
                              height: 53.33.h,
                              child: TextField(
                                controller: controller.phoneController,
                                keyboardType: TextInputType.phone,
                                maxLength: 11,
                                inputFormatters: [
                                  FilteringTextInputFormatter.digitsOnly,
                                ],
                                decoration: InputDecoration(
                                  hintText: '请输入您的手机号',
                                  counterText: '',
                                  hintStyle: TextStyle(
                                    fontSize: 14.67.sp,
                                    color: const Color(0xFF8C8C8C),
                                  ),
                                  border: UnderlineInputBorder(
                                    borderSide: BorderSide(
                                      color: const Color(0xFFD9D9D9),
                                      width: 0.33.r,
                                    ),
                                  ),
                                  enabledBorder: UnderlineInputBorder(
                                    borderSide: BorderSide(
                                      color: const Color(0xFFD9D9D9),
                                      width: 0.33.r,
                                    ),
                                  ),
                                  focusedBorder: UnderlineInputBorder(
                                    borderSide: BorderSide(
                                      color: const Color(0xFFD9D9D9),
                                      width: 0.33.r,
                                    ),
                                  ),
                                  contentPadding:
                                      EdgeInsets.symmetric(vertical: 16.r),
                                  isDense: true,
                                ),
                                onChanged: controller.validatePhone,
                              ),
                            ),
                            SizedBox(height: 24.h),
                            // 验证码标题
                            Row(
                              children: [
                                Image.asset(
                                  'assets/images/icon_edit_password.webp',
                                  width: 18.r,
                                  height: 18.r,
                                ),
                                SizedBox(width: 4.w),
                                Text(
                                  '验证码',
                                  style: TextStyle(
                                    fontSize: 18.sp,
                                    fontWeight: FontWeight.w500,
                                    color: const Color(0xFF262626),
                                  ),
                                ),
                              ],
                            ),
                            // SizedBox(height: 16.33),
                            // 验证码输入区域
                            Container(
                              height: 53.33.h,
                              decoration: BoxDecoration(
                                border: Border(
                                  bottom: BorderSide(
                                    color: const Color(0xFFD9D9D9),
                                    width: 0.33.r,
                                  ),
                                ),
                              ),
                              child: Row(
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: [
                                  // 验证码输入框
                                  Expanded(
                                    child: TextField(
                                      controller: controller.smsCodeController,
                                      keyboardType: TextInputType.number,
                                      maxLength: 6,
                                      inputFormatters: [
                                        FilteringTextInputFormatter.digitsOnly,
                                      ],
                                      decoration: InputDecoration(
                                        hintText: '请输入验证码',
                                        counterText: '',
                                        hintStyle: TextStyle(
                                          fontSize: 14.67.sp,
                                          color: const Color(0xFF8C8C8C),
                                        ),
                                        border: InputBorder.none,
                                        enabledBorder: InputBorder.none,
                                        focusedBorder: InputBorder.none,
                                        contentPadding: EdgeInsets.zero,
                                        isDense: true,
                                      ),
                                      onChanged: controller.validateSmsCode,
                                    ),
                                  ),
                                  SizedBox(width: 16.w),
                                  // 获取验证码按钮
                                  Obx(() => SizedBox(
                                        width: 140.w,
                                        child: TextButton(
                                          style: TextButton.styleFrom(
                                            padding: EdgeInsets.zero,
                                            minimumSize: Size.zero,
                                            alignment: Alignment.center,
                                          ),
                                          onPressed: controller
                                                  .isCountingDown.value
                                              ? null
                                              : controller.sendVerificationCode,
                                          child: Text(
                                            controller.isCountingDown.value
                                                ? '${controller.countdown.value}秒后重新获取'
                                                : controller.countdown.value ==
                                                        60
                                                    ? '获取验证码'
                                                    : '重新发送',
                                            style: TextStyle(
                                              fontSize: 14.67.sp,
                                              color: const Color(0xFFED6C00),
                                            ),
                                          ),
                                        ),
                                      )),
                                ],
                              ),
                            ),
                            SizedBox(height: 10.h),
                            Obx(() => Text(
                                  controller.tipStr.value,
                                  style: TextStyle(
                                      color: Color(0xFFFF3A3A),
                                      fontSize: 14.67),
                                )),
                            SizedBox(height: 20.h),

                            SizedBox(height: 30.h),
                            SizedBox(
                              width: double.infinity,
                              height: 50.h,
                              child: Container(
                                decoration: BoxDecoration(
                                  color: const Color(0xFFED6C00),
                                  borderRadius: BorderRadius.circular(22.33.r),
                                ),
                                child: Material(
                                  color: Colors.transparent,
                                  child: InkWell(
                                    onTap: controller.login,
                                    borderRadius:
                                        BorderRadius.circular(22.33.r),
                                    child: Center(
                                      child: Text(
                                        '登录',
                                        style: TextStyle(
                                          fontSize: 18.sp,
                                          color: Colors.white,
                                          fontWeight: FontWeight.normal,
                                        ),
                                      ),
                                    ),
                                  ),
                                ),
                              ),
                            ),
                            SizedBox(height: 20.h),

                            // 注册按钮
                            if (Platform.isIOS)
                              SizedBox(
                                width: double.infinity,
                                height: 50.h,
                                child: Container(
                                  decoration: BoxDecoration(
                                    color: Colors.white,
                                    border: Border.all(
                                      color: const Color(0xFFED6C00),
                                      width: 1.r,
                                    ),
                                    borderRadius:
                                        BorderRadius.circular(22.33.r),
                                  ),
                                  child: Material(
                                    color: Colors.transparent,
                                    child: InkWell(
                                      onTap: controller.goToRegister,
                                      borderRadius:
                                          BorderRadius.circular(22.33.r),
                                      child: Center(
                                        child: Text(
                                          '注册',
                                          style: TextStyle(
                                            fontSize: 18.sp,
                                            color: const Color(0xFFED6C00),
                                            fontWeight: FontWeight.normal,
                                          ),
                                        ),
                                      ),
                                    ),
                                  ),
                                ),
                              ),

                            SizedBox(height: 40.h),

                            // 用户协议勾选
                            Row(
                              children: [
                                Obx(() => Transform.scale(
                                      scale: 0.9,
                                      child: Checkbox(
                                        value:
                                            controller.isAgreementChecked.value,
                                        onChanged: (_) =>
                                            controller.toggleAgreement(),
                                        activeColor: const Color(0xFFED6C00),
                                        shape: RoundedRectangleBorder(
                                          borderRadius:
                                              BorderRadius.circular(4.0),
                                        ),
                                        side: BorderSide(
                                          color: const Color(0xFFD9D9D9),
                                          width: 1.0,
                                        ),
                                      ),
                                    )),
                                Expanded(
                                  child: GestureDetector(
                                    onTap: () =>
                                        Get.toNamed(Routes.PrivacyPolicy),
                                    child: RichText(
                                      text: TextSpan(
                                        children: [
                                          TextSpan(
                                            text: '我已阅读并同意',
                                            style: TextStyle(
                                              fontSize: 13.33.sp,
                                              color: Colours.text_grey,
                                            ),
                                          ),
                                          TextSpan(
                                            text: '《用户服务与隐私协议》',
                                            style: TextStyle(
                                              fontSize: 13.33.sp,
                                              color: Colours.app_main_orange,
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                  ),
                                ),
                              ],
                            ),
                            // 添加额外的底部空间，确保键盘弹出时有足够的滚动空间
                            SizedBox(height: 120.h),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
